import { _decorator, Component, EventTouch, Node, Sprite, SpriteAtlas, SpriteFrame, UITransform, Vec3 } from 'cc'
import { RandomByRange } from '../../Utils/UtilsIndex'
import { popupBagUI } from './popupBagUI'
import { ConfigSelectItemData } from './ConfigSelectItemData'
const { ccclass, property } = _decorator

@ccclass('WeapItem')
export class WeapItem extends Component {
  private m_atlas: SpriteAtlas = null

  itemType = 0

  bagUI: Node = null
  private selectedItem: Node = null

  sizeMap: number[] = []
  /**开始的位置 */
  startPos: Vec3 = new Vec3()

  /**是否可以放置 */
  _iscanPlaced = false
  /**是否已经放置 */
  _isPlaced = false

  /**放置de位置 */
  _setPos: Vec3 = new Vec3()

  /**放置格子的下表数组 */
  _setPosIndex: number[] = []
  _prePosIndex: number[] = []

  /**是否可以升级 */
  public canUpgrade = false

  public m_selectItemData: ConfigSelectItemData = null

  set preposIndex(value) {
    this._prePosIndex = value
  }
  get preposIndex() {
    return this._prePosIndex
  }

  set setPosIndex(value) {
    this._setPosIndex = value
  }
  get setPosIndex() {
    return this._setPosIndex
  }

  get setPos() {
    return this._setPos
  }
  set setPos(value) {
    this._setPos = value
  }

  get iscanPlaced() {
    return this._iscanPlaced
  }
  set iscanPlaced(value) {
    this._iscanPlaced = value
  }
  get isPlaced() {
    return this._isPlaced
  }
  set isPlaced(value) {
    this._isPlaced = value
  }

  /**获取占Bagitem的个数 */
  start() {
    this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this)
    this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this)
    this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this)
    this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this)
  }
  createItem(ItemData: ConfigSelectItemData) {
    this.m_atlas = ItemData.atals
    this.bagUI = ItemData.bagNode
    this.setData(ItemData)
    //sizeMap以*号分隔
    const sizeMapStr = this.m_selectItemData.sizeMap
    const arr = sizeMapStr.split('*')
    for (let i = 0; i < arr.length; i++) {
      this.sizeMap[i] = parseInt(arr[i])
    }

    //设置旋转角度
    this.node.eulerAngles = new Vec3(0, 0, RandomByRange(0, 85))
    this.node.setScale(new Vec3(this.m_selectItemData.nodeScale, this.m_selectItemData.nodeScale, 1))
    this.node.setPosition(new Vec3(this.m_selectItemData.posI * 106 * 1.5 - 106 * 2.5, 0, 0))

    this.startPos = this.node.getPosition()
  }
  private setData(ItemData: ConfigSelectItemData) {
    this.m_selectItemData = ItemData
    this.itemType = this.m_selectItemData.type

    const sprite = this.node.getComponent(Sprite)
    let spriteframe = this.m_selectItemData.icon
    sprite.spriteFrame = this.m_atlas.getSpriteFrame(spriteframe)
  }
  UpGradeData(ItemData: ConfigSelectItemData) {
    this.setData(ItemData)
  }
  getSizeMap() {
    return this.sizeMap
  }
  //回到起始位置
  fuWei() {
    this.selectedItem = null //是否点中
    this.node.setPosition(this.startPos)
  }
  private onTouchStart(event: EventTouch) {
    //恢复角度
    // if (this.isPlaced) return
    this.node.eulerAngles = new Vec3(0, 0, 0)
    const target = event.target as Node
    if (target) {
      this.selectedItem = target
    }
  }

  private onTouchMove(event: EventTouch) {
    if (this.selectedItem) {
      const delta = event.getDelta()
      const pos = this.selectedItem.getPosition()
      this.selectedItem.setPosition(new Vec3(pos.x + delta.x, pos.y + delta.y, pos.z))
      if (this.itemType == 0) {
        return
      }
      const comp = this.bagUI.getComponent(popupBagUI)

      comp.highlightGrid(this.selectedItem)
    }
  }

  private onTouchEnd(event: EventTouch) {
    if (this.selectedItem) {
      const comp = this.bagUI.getComponent(popupBagUI)
      if (this.iscanPlaced) {
        // this.startNodeParent = this.node.parent
        comp.placeEnd(this.node)
      } else {
        //匹配放置的位置
        this.fuWei()
      }
      this.selectedItem = null
    }
  }

  update(deltaTime: number) {}
}
